package com.zha.boss.mvc.controller;

import com.zha.boss.enums.resp.RespCode;
import com.zha.boss.mvc.models.dto.LoginResponseDto;
import com.zha.boss.mvc.models.forms.LoginRequestForm;
import com.zha.boss.mvc.models.menu.SubMenu;
import com.zha.boss.mvc.models.resp.AjaxResult;
import com.zha.boss.mvc.service.OperatorService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: zha
 * @CreateTime: 2024年09月05日 10:31
 */
@CrossOrigin
@RestController
public class OperatorController {
    @Resource
    private OperatorService operatorService;
    /**
     *  操作员登录
     * @param loginRequestForm  登录的请求信息
     */
    @PostMapping("/operator/access")
    public AjaxResult operatorAccess(@RequestBody LoginRequestForm loginRequestForm) {

        // 1. 访问数据库验证登录信息
        LoginResponseDto responseDto = operatorService.operatorLogin(loginRequestForm);

        // 2. 返回登录的结果
        return AjaxResult.ok(responseDto);
    }

    @GetMapping("/operator/menus")
    public AjaxResult operatorMenus(@RequestHeader Long operatorId) {
        if (operatorId != null && operatorId > 0){
            List<SubMenu> subMenuList = operatorService.operatorMenuList(operatorId);
            return AjaxResult.ok(subMenuList);
        }
        return AjaxResult.warn(RespCode.REQUEST_PARAM_ERR);
    }
    @GetMapping("/operator/logout")
    public AjaxResult operatorLogout(@RequestHeader Long operatorId) {
        operatorService.operatorLogout(operatorId);
        return AjaxResult.ok();
    }
}
